"""
Case Type   : bugfix
Case Name   : 分区键表达式场景，打开enable_partition_opfusion走opfusion
Create At   : 2025/6/12
Owner       : liu-tong-8848
Description :
    1、建表
    2、打开enable_opfusion，打开enable_partition_opfusion;
    3、插入数据
    4、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbBmode')
        self.user_node = Node('PrimaryDbBmode')
        self.constant = Constant()

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1:建表；expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop table if exists t_fix_101;" \
                  + f"create table t_fix_101 (monitor_time timestamp(6) without time zone) " \
                  + f"PARTITION BY RANGE (to_days(monitor_time)) " \
                  + f"(PARTITION p20210801 VALUES LESS THAN (738369), PARTITION p20210802 VALUES LESS THAN (738370)," \
                  + f"PARTITION p20210803 VALUES LESS THAN (738371), PARTITION p20210804 VALUES LESS THAN (738372)," \
                  + f"PARTITION p20210805 VALUES LESS THAN (738373), PARTITION p20210806 VALUES LESS THAN (738374)," \
                  + f"PARTITION p20210807 VALUES LESS THAN (738375), PARTITION p20210808 VALUES LESS THAN (738376)," \
                  + f"PARTITION p20210809 VALUES LESS THAN (738377));"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, sql_res, '执行失败' + text)

        text = '----step2:打开enable_opfusion，打开enable_partition_opfusion；expect:成功----'
        self.log.info(text)
        sql_cmd = f"set enable_opfusion to on; set enable_partition_opfusion to on; " \
                  + f"insert into t_fix_101 values('2021-08-05 11:50:14.000000');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)

        text = '----step3:插入数据；expect:成功----'
        self.log.info(text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, sql_res, '执行失败' + text)

    def tearDown(self):
        text = '----step4:清理环境；expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop table if exists t_fix_101 cascade;" \
                  + f"set enable_opfusion to on;" \
                  + f"set enable_partition_opfusion to off;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.log.info(f'----{os.path.basename(__file__)} end-----')
